// 添加页面的操作
import MyPlugin from "../../../main";
import {Notice, TFile} from "obsidian";
import CryptoJS from "crypto-js";
import KeyModal from "./KeyModal";

export function 文件加密(this_:MyPlugin){
    // 右键加密文件菜单
    this_.registerEvent(
        this_.app.workspace.on('file-menu', (menu, file) => {
            if (file instanceof TFile) {
                // 加密菜单项
                menu.addItem((item) =>
                    item
                        .setTitle('sjz-加密当前文件')
                        .onClick(() => encryptFile(file,this_))
                );
                // 解密菜单项
                menu.addItem((item) =>
                    item
                        .setTitle('sjz-解密当前文件')
                        .onClick(() => decryptFile(file,this_))
                );
            }
        })
    );

    this_.addCommand({
        id: 'encrypt-current-file1',
        name: '加密1',
        callback: () => {
            const activeFile = this_.app.workspace.getActiveFile();
            if (activeFile) {
                this.decryptFile1(activeFile);
            } else {
                new Notice('No file is open.');
            }
        },
    });

    this_.addCommand({
        id: 'decrypt-current-file1',
        name: '解密1',
        callback: () => {
            const activeFile = this_.app.workspace.getActiveFile();
            if (activeFile) {
                this.decryptFile1(activeFile);
            } else {
                new Notice('No file is open.');
            }
        },
    });

    // 加密当前文件
    this_.addCommand({
        id: 'encrypt-current-file',
        name: '加密当前文件',
        callback: () => encryptCurrentFile(this_),
    });

    // 解密解密当前文件
    this_.addCommand({
        id: 'decrypt-current-file',
        name: 'Decrypt Current File',
        callback: () => decryptCurrentFile(this_),
    });

    // // 解密当前文件
    // function decryptCurrentFile(this_:MyPlugin) {
    //     const activeFile = this_.app.workspace.getActiveFile();
    //     if (activeFile) {
    //         decryptFile(activeFile,this_);
    //     }
    // }

    // 加密当前文件
    function encryptCurrentFile(this_:MyPlugin) {
        const activeFile = this_.app.workspace.getActiveFile();
        if (activeFile) {
            encryptFile(activeFile,this_);
        }
    }

    // 加密当前文件
    function encryptFile(file: TFile,this_:MyPlugin) {
        const fileContent = this.app.vault.read(file);
        const encryptedContent = CryptoJS.AES.encrypt(fileContent, 'your-secret-key').toString();
        this.app.vault.modify(file, encryptedContent);
    }

    // 解密当前文件
    function decryptFile(file: TFile,this_:MyPlugin) {
        // const fileContent = this_.app.vault.read(file);
        try {
            const decryptedBytes = CryptoJS.AES.decrypt("fileContent", 'your-secret-key');
            const decryptedContent = decryptedBytes.toString(CryptoJS.enc.Utf8);

            if (decryptedContent) {
                this_.app.vault.modify(file, decryptedContent);
            } else {
                new Notice('File decryption failed.');
            }
        } catch (e) {
            new Notice('Error during decryption.');
        }
    }

    // 解密当前文件
   function decryptCurrentFile(this_:MyPlugin) {
        const activeFile = this_.app.workspace.getActiveFile();
        if (activeFile) {
            decryptFile(activeFile,this_);
        }
    }


}

export async function decryptFile1(file: TFile) {
    if (!file) {
        new Notice('No file is open.');
        return;
    }

    new KeyModal(this.app, async (key) => {
        const fileContent = await this.app.vault.read(file);
        try {
            const decryptedBytes = CryptoJS.AES.decrypt(fileContent, key);
            const decryptedContent = decryptedBytes.toString(CryptoJS.enc.Utf8);
            if (decryptedContent) {
                await this.app.vault.modify(file, decryptedContent);
                new Notice('File decrypted.');
            } else {
                new Notice('Decryption failed. Invalid key?');
            }
        } catch (e) {
            new Notice('Error during decryption.');
        }
    }).open();
}

export async function encryptFile1(file: TFile) {
    if (!file) {
        new Notice('No file is open.');
        return;
    }

    new KeyModal(this.app, async (key) => {
        const fileContent = await this.app.vault.read(file);
        const encryptedContent = CryptoJS.AES.encrypt(fileContent, key).toString();
        await this.app.vault.modify(file, encryptedContent);
        new Notice('File encrypted.');
    }).open();
}


